﻿using System;
using Microsoft.AspNetCore.Mvc;
using RayPI.Token;
using RayPI.Token.Model;

namespace RayPI.Controllers.Admin
{
    /// <summary>
    /// token令牌操作模块
    /// </summary>
    [Route("api/System/[controller]")]
    [ApiController]
    public class TokenController : Controller
    {
        /// <summary>
        /// 获得token
        /// </summary>
        /// <param name="id">用户id</param>
        /// <param name="sub">身份</param>
        /// <param name="expiresSliding">相对过期时间，单位为分</param>
        /// <param name="expiresAbsolute">绝对过期时间，单位为天</param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetToken(long id, string sub, int expiresSliding, int expiresAbsolute)
        {
            TokenModel tokenModel = new TokenModel();
            tokenModel.Sub = sub;
            tokenModel.Uid = id;

            TimeSpan expiresSlidingTimeSpan = TimeSpan.FromMinutes(expiresSliding);
            TimeSpan expiresAbsoluteTimeSpan = TimeSpan.FromDays(expiresAbsolute);

            string issueJWT = RayPIToken.IssueJWT(tokenModel, expiresSlidingTimeSpan, expiresAbsoluteTimeSpan);

            return Json(issueJWT);
        }


    }
}